package com.pxmeta.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.party.entity.Association;
import com.pxmeta.party.entity.AssociationUser;
import com.pxmeta.party.entity.Party;
import com.pxmeta.party.entity.dto.CreateAssociationDTO;
import com.pxmeta.party.entity.dto.UpdateOwnerDTO;
import com.pxmeta.party.service.IAssociationService;
import com.pxmeta.party.service.IAssociationUserService;
import com.pxmeta.party.service.IPartyService;
import com.pxmeta.user.entity.vo.PartyUserBillVO;
import com.pxmeta.user.service.IUserBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Api(value = "公会管理",tags = "公会管理")
@RequiredArgsConstructor
@RequestMapping("/association")
@CrossOrigin
public class AssociationController {

    private final IAssociationService associationService;
    private final IPartyService partyService;
    private final IUserBillService userBillService;
    private final IAssociationUserService associationUserService;

    @GetMapping("/getAssociationList")
    @ApiOperation("获取公会列表")
    public AjaxResult getAssociationList(@RequestParam Integer page,
                                         @RequestParam Integer size){
        Page<Association> result = associationService.getAssociationPage(page, size);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @PostMapping("/createAssociation")
    @ApiOperation("创建公会")
    public AjaxResult createAssociation(@RequestBody CreateAssociationDTO dto){
        associationService.createAssociation(dto);
        return AjaxResult.success();
    }

    @GetMapping("/getContributionList")
    @ApiOperation("获取房间贡献榜单")
    public AjaxResult getContributionList(@RequestParam Long partyId, @RequestParam Integer type ){
        List<PartyUserBillVO> list = userBillService.getContributionList(partyId, type);
        return AjaxResult.success(list);
    }

    @GetMapping("/getAssociationPartyList")
    @ApiOperation("获取公会房间列表")
    public AjaxResult<List<Party>> getAssociationPartyList(@RequestParam Integer page,
                                                           @RequestParam Integer size,
                                                           @RequestParam Long associationId){
        IPage<Party> result = partyService.getAssociationPartyList(page, size,associationId,null);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @PutMapping("/setAssociationAdmin")
    @ApiOperation("设置公会管理员")
    public AjaxResult setAssociationAdmin(@RequestParam Long associationId,
                                          @RequestParam Long userId){
        AssociationUser associationUser = associationUserService.getUserSign(userId);
        if (!associationId.equals(associationUser.getAssociationId())){
            return AjaxResult.error();
        }
        associationUser.setIsAdmin(Boolean.TRUE);
        associationUserService.updateById(associationUser);
        return AjaxResult.success();
    }


    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("根据id删除公会")
    public AjaxResult deleteById(@PathVariable Long id){
        associationService.deleteById(id);
        return AjaxResult.success();
    }


    @PutMapping("/updateOwner")
    @ApiOperation("修改公会长")
    public AjaxResult updateOwner(@RequestBody UpdateOwnerDTO dto){
        associationService.updateOwner(dto.getAssociationId(),dto.getOwnerId());
        return AjaxResult.success();
    }



    @PutMapping("/cancelAssociationAdmin")
    @ApiOperation("取消公会管理")
    public AjaxResult cancelAssociationAdmin(@RequestParam Long associationId,
                                             @RequestParam Long userId){

        AssociationUser associationUser = associationUserService.getUserSign(userId);
        if (!associationId.equals(associationUser.getAssociationId())){
            return AjaxResult.error();
        }
        associationUser.setIsAdmin(Boolean.FALSE);
        associationUserService.updateById(associationUser);
        return AjaxResult.success();
    }
}
